package com.exercise.animations.viewgroup

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout.LayoutParams
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.exercise.animations.databinding.ActivityViewGroupBinding
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.google.android.material.snackbar.Snackbar
import kotlin.random.Random

class ViewGroupActivity : BaseActivity<BaseViewModel, ActivityViewGroupBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityViewGroupBinding
        get() = ActivityViewGroupBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun initData() {
        binding.remove.setOnClickListener {
            if (binding.verticalContainer.childCount > 0) {
                binding.verticalContainer.removeViewAt(
                    Random(System.currentTimeMillis()).nextInt(
                        binding.verticalContainer.childCount
                    )
                )
            } else {

                Snackbar.make(it, "没孩子了", Snackbar.LENGTH_SHORT).show()
            }
        }
    }

    val TAG = "ViewGroupActivity"
    override fun initView() {
        binding.add.setOnClickListener {
            val nextInt = if (binding.verticalContainer.childCount > 0) {
                Random(System.currentTimeMillis()).nextInt(
                    binding.verticalContainer.childCount
                )
            } else {
                0
            }

            Log.d(TAG, "initView: $nextInt")
            binding.verticalContainer.addView(
                createChildView(),
                nextInt
            )
        }
        binding.btnVisible.setOnClickListener {
            val childCount = binding.verticalContainer.childCount
            if (childCount > 0){
                binding.verticalContainer.getChildAt(0).visibility = View.VISIBLE
            }else{
                Snackbar.make(it, "没孩子了", Snackbar.LENGTH_SHORT).show()
            }
        }
        binding.btnGone.setOnClickListener {
            val childCount = binding.verticalContainer.childCount
            if (childCount > 0){
                binding.verticalContainer.getChildAt(0).visibility = View.GONE
            }else{
                Snackbar.make(it, "没孩子了", Snackbar.LENGTH_SHORT).show()

            }
        }
    }

    private fun createChildView(): View {
        val childCount = binding.verticalContainer.childCount
        return TextView(this).apply {
            layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
            setPadding(20, 10, 20, 10)
            text = "$childCount"
            gravity = Gravity.CENTER
            setBackgroundColor(ContextCompat.getColor(context, android.R.color.holo_orange_dark))
        }
    }

    companion object {
        fun start(context: Context) {
            Intent(context, ViewGroupActivity::class.java).also {
                context.startActivity(it)
            }
        }
    }
}